package scsys.webServlet.main;

import com.alibaba.fastjson.JSON;
import scsys.pojo.Course;
import scsys.pojo.userCourse;
import scsys.servlet.CourseService;
import scsys.servlet.CourseServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/add_course_Servlet")
public class add_course_Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        CourseService courseService = new CourseServiceImpl();

        //1. 接收数据
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为User对象
        userCourse userCourse = JSON.parseObject(params, userCourse.class);


        // 查询是否存在该课程
        Course course = new Course();
        course.setId(userCourse.getCourseId());
        Course sele_course = courseService.selectOne(course);
        if(sele_course == null){// 不存在该课程 将课程添加到数据库上
            Course course1 = new Course();
            course1.setId(userCourse.getCourseId());
            course1.setName(userCourse.getCourseName());
            course1.setTeacher(userCourse.getTeacher());
            course1.setPoint(userCourse.getPoint());
            course1.setTime1(userCourse.getTime1());
            course1.setTime2(userCourse.getTime2());
            course1.setLocation(userCourse.getLocation());
            course1.setLimited(userCourse.getLimited());
            courseService.add(course1);
            course1 = null;
            // 添加成功信号
            response.getWriter().write("yes");
        }else {
            // 已经存在信号
            response.getWriter().write("no");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
